# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require 'SOD/tools/Tool'

module SOD
  
  class SelectFootprintTool < Tool
    
    def initialize
      @cursor = UI.create_cursor(Plugin.dir + "/icons/test.tiff", 3, 3)
    end


    def activate
      super
      Sketchup.active_model.selection.clear
    end
    
    def onMouseMove(flags, x, y, view)
      super
      # Should apply user's precision setting here   --automatically done, I think
      # Also:  show relative coordinates?
      Sketchup.set_status_text("Select a face to become the New Footprint = " + @ip.position.to_s)
      view.tooltip = "New Footprint"
    end


    def onLButtonUp(flags, x, y, view)
      super
      
      model = Sketchup.active_model
      active_path = model.active_path
      
      if not active_path.nil?
        UI.messagebox("Footprint should be added at the top level of a SketchUp model")
        Sketchup.send_action("selectSelectionTool:")
        return false
      end
      
      if (face = @ip.face)
        puts "Face detected" if $debug
        
        Plugin.model.footprint.add(face)
        
        # Always want to end with the Line Tool so users can modify the New Footprint.
        #Sketchup.send_action("selectLineTool:")
        
        #$g_footprint = face #I think the global variables have to be stored in a Hash. Then they can be accessed for multiple rectangular footprints
        #loop = face.outer_loop
        #pnt0 = loop.vertices[0].position
        #pnt2 = loop.vertices[2].position
        #vec = pnt2 - pnt0
        #g_set_footprint_size(vec.x.to_mm.abs, vec.y.to_mm.abs)
        #-> moved to FootprintObserver to update on Footprint change!
        
      else
        puts "No Face detected" if $debug
        
      end
      Sketchup.send_action("selectSelectionTool:")
      
    end
    
  end
  
end
